// secondactivity.js
var app = getApp()
var shihan = require('../../common/fetch')
var config = require('../../common/config')
var goodstimer
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showLoading: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    if (options.share) {
      that.setData({
        sharestr: options.share || '',
        weburl: config.commonurl + 'h5/index/seconds_activity.html?share=' + options.share
      })
    } else {
      that.setData({
        weburl: config.commonurl + 'h5/index/seconds_activity.html'
      })
    }

    if (options.pid) {
      config.partner_id = options.pid
    }
    if (options.tit) {
      that.setData({
        sharetit: options.sharetit || '',
        shareimg: options.shareimg || '',
        shareurl: options.shareurl || ''
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    console.log(this.data)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    clearInterval(goodstimer);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function(options) {
    var sharestr, shareurl, sharetit = '';
    var that = this
    var customerId = wx.getStorageSync('customerId') || ''
    if (that.data.shareurl) { //?share=9dcb6b51ab3eed95
      var shareurl = decodeURIComponent(that.data.shareurl)
      var str = shareurl.split('?')[1].split('=');
      if (str[0] == 'share') {
        sharestr = str[1]
      }
    } else {
      sharestr = that.data.sharestr
    }
    if (that.data.sharetit) {
      sharetit = that.data.sharetit
    }
    shareurl = '/pages/secondactivity/secondactivity?tit=' + sharetit + '&pid=' + customerId + '&share=' + sharestr
    // console.log(options.webViewUrl)
    return {
      title: sharetit,
      path: shareurl,
      // imageUrl: that.data.shareimg,
      success: function(res) {
        // 转发成功
        shihan.sendRequest(config.share_url, 'POST', {
            url: that.data.shareurl
          })
          .then(function(response) {
            // console.log(response.data);
          }, function(error) {
            console.log(error);
          });
      },
      fail: function(res) {
        // 转发失败
      }
    }
  }
})